/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd.            */
/*                                                                 */
/*  NAME      = openwire_connectioncontrol_tightUnmarshal.c                      */
/*  NOTE      =                                                    */
/*  DATE      = 2015/05/25 by geyh                                 */
/*******************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frmem.h"
#include "frcomm.h"
#include "frbyte_buffer.h"
#include "../../src/module/activemq/activemq_common.h"
#include "../../src/module/activemq/activemq_openwire_parse.h"
#include "../../src/module/activemq/activemq_base_type.h"
#include "../../src/module/activemq/activemq_core.h"
#include "apmtest_cunit.h"
#include "../../../src/module/activemq/config.h"
#include "../comm/apmtest_comm.h"

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

L7session_activeMQ_private* __activeMQ_private_create(void);

static void __case1(void)
{
    Frbyte_Packet *p_st_data = NULL;
    int i_ret = 0;
    FILE *fp_file = NULL;
    char *p_data_tmp = NULL;
    int i_data_len = 0;
    L7session_activeMQ_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    i_ret = apmtest_create_ct(&p_st_conn, activeMQ_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 61616);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    fp_file = fopen("t_activemq_openwire_parse/test_data/tight_cache/connectioncontrol", "rb");

    fseek(fp_file, 0, SEEK_END);
    i_data_len = ftell(fp_file);
    rewind(fp_file);

    p_data_tmp = (char *)frmalloc(i_data_len);

    fread(p_data_tmp, 1, i_data_len, fp_file);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data_tmp, i_data_len);
    frfree(p_data_tmp);
    bytebuff_skip(p_st_data,5);

    p_st_private = __activeMQ_private_create();
    p_st_private->i_tightEncodingEnabled = 1;
    p_st_private->i_cacheEnabled = 1;

    i_ret = openwire_connectioncontrol_tightUnmarshal(p_st_data, p_st_body_data, p_st_private);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    activeMQ_session_destroy(p_st_private);
    bytebuff_free(&p_st_data);
    fclose(fp_file);
    frreport_body_free(&p_st_body_data);
    apmtest_destroy_ct(&p_st_conn);
    return;

}

static void __case2(void)
{
    Frbyte_Packet *p_st_data = NULL;
    int i_ret = 0;
    FILE *fp_file = NULL;
    char *p_data_tmp = NULL;
    int i_data_len = 0;
    L7session_activeMQ_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    i_ret = apmtest_create_ct(&p_st_conn, activeMQ_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 61616);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    fp_file = fopen("t_activemq_openwire_parse/test_data/tight_uncache/connectioncontrol", "rb");

    fseek(fp_file, 0, SEEK_END);
    i_data_len = ftell(fp_file);
    rewind(fp_file);

    p_data_tmp = (char *)frmalloc(i_data_len);

    fread(p_data_tmp, 1, i_data_len, fp_file);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data_tmp, i_data_len);
    frfree(p_data_tmp);
    bytebuff_skip(p_st_data,5);

    p_st_private = __activeMQ_private_create();
    p_st_private->i_tightEncodingEnabled = 1;
    p_st_private->i_cacheEnabled = 0;

    i_ret = openwire_connectioncontrol_tightUnmarshal(p_st_data, p_st_body_data, p_st_private);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    activeMQ_session_destroy(p_st_private);
    bytebuff_free(&p_st_data);
    fclose(fp_file);
    frreport_body_free(&p_st_body_data);
    apmtest_destroy_ct(&p_st_conn);
    return;

}

static void __case3(void)
{
    Frbyte_Packet *p_st_data = NULL;
    int i_ret = 0;
    FILE *fp_file = NULL;
    char *p_data_tmp = NULL;
    int i_data_len = 0;
    L7session_activeMQ_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    i_ret = apmtest_create_ct(&p_st_conn, activeMQ_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 61616);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    fp_file = fopen("t_activemq_openwire_parse/test_data/loose_cache/connectioncontrol", "rb");

    fseek(fp_file, 0, SEEK_END);
    i_data_len = ftell(fp_file);
    rewind(fp_file);

    p_data_tmp = (char *)frmalloc(i_data_len);

    fread(p_data_tmp, 1, i_data_len, fp_file);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data_tmp, i_data_len);
    frfree(p_data_tmp);
    bytebuff_skip(p_st_data,5);

    p_st_private = __activeMQ_private_create();
    p_st_private->i_tightEncodingEnabled = 0;
    p_st_private->i_cacheEnabled = 1;

    i_ret = openwire_connectioncontrol_tightUnmarshal(p_st_data, p_st_body_data, p_st_private);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    activeMQ_session_destroy(p_st_private);
    bytebuff_free(&p_st_data);
    fclose(fp_file);
    frreport_body_free(&p_st_body_data);
    apmtest_destroy_ct(&p_st_conn);
    return;

}

static void __case4(void)
{
    Frbyte_Packet *p_st_data = NULL;
    int i_ret = 0;
    FILE *fp_file = NULL;
    char *p_data_tmp = NULL;
    int i_data_len = 0;
    L7session_activeMQ_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    i_ret = apmtest_create_ct(&p_st_conn, activeMQ_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 61616);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    fp_file = fopen("t_activemq_openwire_parse/test_data/loose_uncache/connectioncontrol", "rb");

    fseek(fp_file, 0, SEEK_END);
    i_data_len = ftell(fp_file);
    rewind(fp_file);

    p_data_tmp = (char *)frmalloc(i_data_len);

    fread(p_data_tmp, 1, i_data_len, fp_file);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data_tmp, i_data_len);
    frfree(p_data_tmp);
    bytebuff_skip(p_st_data,5);

    p_st_private = __activeMQ_private_create();
    p_st_private->i_tightEncodingEnabled = 0;
    p_st_private->i_cacheEnabled = 0;

    i_ret = openwire_connectioncontrol_tightUnmarshal(p_st_data, p_st_body_data, p_st_private);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);

    activeMQ_session_destroy(p_st_private);
    bytebuff_free(&p_st_data);
    fclose(fp_file);
    frreport_body_free(&p_st_body_data);
    apmtest_destroy_ct(&p_st_conn);
    return;

}

static void __case5(void)
{
    Frbyte_Packet *p_st_data = NULL;
    int i_ret = 0;
    L7session_activeMQ_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Conntrack *p_st_conn = NULL;

    i_ret = apmtest_create_ct(&p_st_conn, activeMQ_session_destroy, (char *)"192.168.0.1", (char *)"192.168.0.35", 5460, 61616);
    p_st_body_data = frreport_body_new_l7(p_st_conn);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *)"\x65\x00\x00", 3);

    p_st_private = __activeMQ_private_create();
    p_st_private->i_tightEncodingEnabled = 0;
    p_st_private->i_cacheEnabled = 0;

    i_ret = openwire_connectioncontrol_tightUnmarshal(p_st_data, p_st_body_data, p_st_private);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    activeMQ_session_destroy(p_st_private);
    bytebuff_free(&p_st_data);

    frreport_body_free(&p_st_body_data);
    apmtest_destroy_ct(&p_st_conn);
    return;

}

int add_suite_openwire_connectioncontrol_tightUnmarshal(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" openwire_connectioncontrol_tightUnmarshal()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);
    CU_add_test(pSuite, "case3", __case3);
    CU_add_test(pSuite, "case4", __case4);
    CU_add_test(pSuite, "case5", __case5);
//    CU_add_test(pSuite, "case6", __case6);
//    CU_add_test(pSuite, "case7", __case7);
//    CU_add_test(pSuite, "case8", __case8);
//    CU_add_test(pSuite, "case9", __case9);
//    CU_add_test(pSuite, "case10", __case10);
//    CU_add_test(pSuite, "case11", __case11);

    return FR_SUCC;

}